<template>
  <div class="app-container calendar-list-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input clearable class="filter-item" style="width: 200px;" placeholder="请输入备注" v-model="listQuery.remark">
      </el-input>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      <el-table-column align="center" label="备注" prop="remark" sortable>
      </el-table-column>

      <el-table-column align="center" label="开始时间" prop="startTime">
      </el-table-column>

      <el-table-column align="center" label="结束时间" prop="endTime">
      </el-table-column>

      <el-table-column align="center" label="拼购数量" prop="leastNum">
      </el-table-column>

      <el-table-column align="center" label="状态栏" prop="runningFlag">
        <template slot-scope="scope">
          <el-tag>{{handleType[scope.row.runningFlag]}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="findDetail(scope.row)">详情</el-button>
          <!-- <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :rules="rules" ref="dataForm" :model="dataForm" status-icon label-position="left" label-width="100px" style='width: 400px; margin-left:50px;'>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="dataForm.remark"></el-input>
        </el-form-item>
        <el-form-item label="拼团时间" prop="startTime">
          <el-date-picker v-model="timeInterval" type="datetimerange" value-format="yyyy-MM-dd HH:mm:ss" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="商品id" prop="goodids">
          <el-input type="textarea" v-model="dataForm.goodids" placeholder="填写商品id，用逗号隔开,如： 1682412,2531561,231516,321651"></el-input>
        </el-form-item>

        <el-form-item label="拼购数量" prop="leastNum">
          <el-input-number :min="0" v-model="dataForm.leastNum"></el-input-number>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="createData">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 170px;
  display: block;
}
.avatars {
  width: 150px;
  height: 170px;
  display: block;
}
</style>

<script>
import { listGp, insertGroup, deleteGroup } from '@/api/group'
import { getToken } from '@/utils/auth'
import { MessageBox } from 'element-ui'

export default {
  name: 'Ad',
  computed: {
    headers() {
      return {
        'X-Token': getToken()
      }
    }
  },
  data() {
    return {
      handleType: ['', '即将开始', '正在上线', '已结束'],
      list: undefined,
      total: undefined,
      timeInterval: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        remark: undefined,
        sort: '+id'
      },
      dataForm: {
        id: undefined,
        remark: undefined,
        leastNum: undefined,
        goodids: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }],
        goodids: [{ required: true, message: '商品id不能为空', trigger: 'blur' }],
        leastNum: [{ required: true, message: '拼购数量不能为空', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      listGp(this.listQuery)
        .then(response => {
          this.list = response.data.data.items
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        id: undefined,
        remark: undefined,
        leastNum: undefined,
        goodids: undefined
      }
      this.timeInterval = undefined
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    findDetail(row) {
      this.$router.push({
        path: '/promotion/group/' + row.id + '/planGroup'
      })
    },

    createData() {
      if (this.timeInterval != null) {
        this.dataForm.startTime = this.timeInterval[0]
        this.dataForm.endTime = this.timeInterval[1]
      } else {
        MessageBox.alert('请选择活动时间', {
          confirmButtonText: '确定',
          type: 'error'
        })
        return
      }
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          insertGroup(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              })
            })
            .catch(res => {
              this.$notify({
                title: '失败',
                message: res.data.errmsg,
                type: 'error',
                duration: 6000
              })
            })
        }
      })
    },

    handleDelete(row) {
      this.$confirm('此操作删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteGroup(row).then(response => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          const index = this.list.indexOf(row)
          this.list.splice(index, 1)
        })
      })
    }
  }
}
</script>
